import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;

public class Teste {

	// error_objeto_existente = masculino
	// error_entidade_existente = feminino

	static void gerarClasseBC(String beanName, boolean masculino, String pastaDeSaida) throws FileNotFoundException, UnsupportedEncodingException,
			IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\TemplateBC.java");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName).replace("MSG_UNICIDADE",
						masculino ? "ERROR_OBJETO_EXISTENTE" : "ERROR_ENTIDADE_EXISTENTE")
						+ "\r\n");
			}
		}
		ManipuladorDeArquivos.salvarArquivo(
				String.format(pastaDeSaida + "java\\br\\gov\\pe\\seres\\siap\\business\\implementation\\" + "%sBC.java", beanName), output.toString());
	}

	static void gerarClasseDAO(String beanName, String pastaDeSaida) throws FileNotFoundException, UnsupportedEncodingException, IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\TemplateDAO.java");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName) + "\r\n");
			}
		}
		ManipuladorDeArquivos.salvarArquivo(
				String.format(pastaDeSaida + "java\\br\\gov\\pe\\seres\\siap\\persistence\\implementation\\" + "%sDAO.java", beanName), output.toString());
	}

	static void gerarEditMB(String beanName, String atributoBean, String nomePojoAlias, String nomePackage, String pastaDeSaida)
			throws FileNotFoundException, UnsupportedEncodingException, IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\TemplateEditMB.java");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName).replace("NOME_ATRIBUTO_BEAN", atributoBean).replace("#ALIAS#", nomePojoAlias)
						.replace("NOME_PACKAGE", nomePackage)
						+ "\r\n");
			}
		}
		File f = new File(pastaDeSaida + "java\\br\\gov\\pe\\seres\\siap\\view\\managedBean\\" + nomePackage);
		if (!f.exists()) {
			f.mkdir();
		}
		ManipuladorDeArquivos.salvarArquivo(
				String.format(pastaDeSaida + "java\\br\\gov\\pe\\seres\\siap\\view\\managedBean\\" + nomePackage + "\\" + "%sEditMB.java", beanName),
				output.toString());
	}

	static void gerarEditXHTML(String beanName, String atributoBean, String pastaDeSaida, String nomeMenu) throws FileNotFoundException,
			UnsupportedEncodingException, IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\TemplateEdit.xhtml");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName).replace("NOME_ATRIBUTO_BEAN", atributoBean).replace("#NOME_MENU#", nomeMenu)
						+ "\r\n");
			}
		}
		File f = new File(pastaDeSaida + "webapp\\private\\pages\\" + atributoBean);
		if (!f.exists()) {
			f.mkdir();
		}
		ManipuladorDeArquivos.salvarArquivo(String.format(pastaDeSaida + "webapp\\private\\pages\\" + atributoBean + "\\%sEdit.xhtml", atributoBean),
				output.toString());
	}

	static void gerarEntradasAlias(String beanName, String atributoBean, String nomePojoAlias) {
		StringBuilder output = new StringBuilder();
		output.append("/**" + "\r\n");
		output.append(String.format(" * Regra de navegacao da consulta de %s.", beanName) + "\r\n");
		output.append(" */");
		output.append(String.format("	public static final String %s_CONSULTA = \"%sConsulta\";", nomePojoAlias, atributoBean) + "\r\n");

		output.append("/**" + "\r\n");
		output.append(String.format(" * Regra de navegacao da edicao de %s.", beanName) + "\r\n");
		output.append("*/" + "\r\n");
		output.append(String.format(" public static final String %s_EDICAO = \"%sEdicao\";", nomePojoAlias, atributoBean) + "\r\n");

		System.out.println(output.toString());
	}

	static void gerarEntradasClassesProperties(String beanName, String nomeClasse) {
		StringBuilder classesProperties = new StringBuilder();
		classesProperties.append(String.format("br.gov.pe.seres.siap.bean.%s=%s\n", beanName, nomeClasse));
		classesProperties.append(String.format("br.gov.pe.seres.siap.bean.%s.descricao=Descri\\u00E7\\u00E3o\n", beanName));
		System.out.println(classesProperties.toString());
	}

	static void gerarEntradasFacesConfig(String beanName, String atributoBean) {
		StringBuilder output = new StringBuilder();
		output.append(String.format("<!-- NAVEGACAO %s -->", beanName) + "\r\n");
		output.append("<navigation-case>" + "\r\n");
		output.append(String.format("	<from-outcome>%sEdicao</from-outcome>", atributoBean) + "\r\n");
		output.append(String.format("	<to-view-id>/private/pages/%s/%sEdit.xhtml</to-view-id>", atributoBean, atributoBean) + "\r\n");
		output.append("	<redirect />" + "\r\n");
		output.append("</navigation-case>" + "\r\n");
		output.append("<navigation-case>" + "\r\n");
		output.append(String.format("	<from-outcome>%sConsulta</from-outcome>", atributoBean) + "\r\n");
		output.append(String.format("	<to-view-id>/private/pages/%s/%sList.xhtml</to-view-id>", atributoBean, atributoBean) + "\r\n");
		output.append("	<redirect />" + "\r\n");
		output.append("</navigation-case>" + "\r\n");

		System.out.println(output.toString());

	}

	static void gerarEntradasFachada(String beanName, String atributoBean, String umUma, String oA) throws FileNotFoundException,
			UnsupportedEncodingException, IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\templateMetodosFachada.txt");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName).replaceAll("NOME_ATRIBUTO_BEAN", atributoBean).replace("UM_UMA", umUma)
						.replace("O_A", oA)
						+ "\r\n");
			}
		}

		System.out.println(output);
	}

	static void gerarEntradasMenu(String beanName, String nomeMenu, String atributoBean) {
		StringBuilder menu = new StringBuilder();
		menu.append(String.format("<p:submenu label=\"#{messages.menu_%s}\"", nomeMenu) + "\r\n");
		menu.append(String.format("		rendered=\"#{securityContext.hasRole('%s')}\">", beanName) + "\r\n");
		menu.append("		<p:menuitem value=\"#{messages.menu_item_incluir}\" " + "\r\n");
		menu.append(String.format("			rendered=\"#{securityContext.hasRole('%s.incluir')}\"", beanName) + "\r\n");
		menu.append(String.format("			action=\"#{%sEditMB.preInserir}\" />", atributoBean) + "\r\n");
		menu.append("		<p:menuitem value=\"#{messages.menu_item_consultar}\" " + "\r\n");
		menu.append(String.format("			rendered=\"#{securityContext.hasRole('%s.consultar')}\"", beanName) + "\r\n");
		menu.append(String.format("			action=\"#{%sListMB.preConsultar}\" />", atributoBean) + "\r\n");
		menu.append("	</p:submenu>" + "\r\n");

		System.out.println(menu.toString());
	}

	static void gerarEntradasMessagesProperties(String atributoBean, String nomeClasse, String menu) {
		StringBuilder messagesProperties = new StringBuilder();
		messagesProperties.append(String.format("#Edit %s\n", nomeClasse));

		messagesProperties.append(String.format("%sEdit_titulo_inserir=Cadastrar %s\r\n", atributoBean, nomeClasse));
		messagesProperties.append(String.format("%sEdit_titulo_alterar=Alterar %s\r\n", atributoBean, nomeClasse));
		messagesProperties.append(String.format("%sEdit_titulo_excluir=Excluir %s\r\n", atributoBean, nomeClasse));
		messagesProperties.append(String.format("%sEdit_titulo_visualizar=Visualizar %s\r\n", atributoBean, nomeClasse));
		messagesProperties.append(String.format("%sEdit_label_descricao=Descri\\u00E7\\u00E3o\r\n", atributoBean));

		messagesProperties.append(String.format("#List %s\r\n", nomeClasse));

		messagesProperties.append(String.format("%sList_titulo=Consultar %s\r\n", atributoBean, nomeClasse));
		messagesProperties.append(String.format("%sList_tabela=Lista de %s\r\n", atributoBean, nomeClasse));
		messagesProperties.append(String.format("%sList_label_descricao=Descri\\u00E7\\u00E3o\r\n", atributoBean));
		messagesProperties.append(String.format("%sList_tabela_descricao=Descri\\u00E7\\u00E3o\r\n", atributoBean));
		messagesProperties.append(String.format("menu_%s=%s\r\n", menu, nomeClasse));

		System.out.println(messagesProperties.toString());

	}

	static void gerarEntradasPersistenceXml(String beanName) {
		String persistenceProperties = String.format("<class>br.gov.pe.seres.siap.bean.%s</class>", beanName);
		System.out.println(persistenceProperties);
	}

	static void gerarFiltro(String beanName, String pastaDeSaida) throws FileNotFoundException, UnsupportedEncodingException, IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\FiltroTemplate.java");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName) + "\r\n");
			}
		}
		ManipuladorDeArquivos.salvarArquivo(
				String.format(pastaDeSaida + "java\\br\\gov\\pe\\seres\\siap\\business\\implementation\\filtro\\" + "Filtro%s.java", beanName),
				output.toString());
	}

	static void gerarInterfaceBC(String beanName, String pastaDeSaida) throws FileNotFoundException, UnsupportedEncodingException, IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\ITemplateBC.java");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName) + "\r\n");
			}
		}
		ManipuladorDeArquivos.salvarArquivo(String.format(pastaDeSaida + "java\\br\\gov\\pe\\seres\\siap\\business\\" + "I%sBC.java", beanName),
				output.toString());
	}

	static void gerarInterfaceDAO(String beanName, String pastaDeSaida) throws FileNotFoundException, UnsupportedEncodingException, IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\ITemplateDAO.java");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName) + "\r\n");
			}
		}
		ManipuladorDeArquivos.salvarArquivo(String.format(pastaDeSaida + "java\\br\\gov\\pe\\seres\\siap\\persistence\\" + "I%sDAO.java", beanName),
				output.toString());

	}

	static void gerarListMB(String beanName, String atributoBean, String nomePojoAlias, String nomePackage, String pastaDeSaida)
			throws FileNotFoundException, UnsupportedEncodingException, IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\TemplateListMB.java");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName).replace("NOME_ATRIBUTO_BEAN", atributoBean).replace("#ALIAS#", nomePojoAlias)
						.replace("NOME_PACKAGE", nomePackage)
						+ "\r\n");
			}
		}
		File f = new File(pastaDeSaida + "java\\br\\gov\\pe\\seres\\siap\\view\\managedBean\\" + nomePackage);
		if (!f.exists()) {
			f.mkdir();
		}
		ManipuladorDeArquivos.salvarArquivo(
				String.format(pastaDeSaida + "java\\br\\gov\\pe\\seres\\siap\\view\\managedBean\\" + nomePackage + "\\" + "%sListMB.java", beanName),
				output.toString());
	}

	static void gerarListXHTML(String beanName, String atributoBean, String pastaDeSaida, String nomeMenu) throws FileNotFoundException,
			UnsupportedEncodingException, IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\TemplateList.xhtml");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName).replace("NOME_ATRIBUTO_BEAN", atributoBean).replace("#NOME_MENU#", nomeMenu)
						+ "\r\n");
			}
		}
		File f = new File(pastaDeSaida + "webapp\\private\\pages\\" + atributoBean);
		if (!f.exists()) {
			f.mkdir();
		}
		ManipuladorDeArquivos.salvarArquivo(String.format(pastaDeSaida + "webapp\\private\\pages\\" + atributoBean + "\\%sList.xhtml", atributoBean),
				output.toString());
	}

	static void gerarPojo(String beanName, String nomeTabela, String pastaDeSaida) throws FileNotFoundException, UnsupportedEncodingException, IOException {
		StringBuilder output = new StringBuilder();
		ManipuladorDeArquivos arquivo = new ManipuladorDeArquivos("templates\\beanTemplate.java");
		Iterator<String> linhas = arquivo.iterator();
		while (linhas.hasNext()) {
			String linha = linhas.next();
			if (linha != null) {
				output.append(linha.replaceAll("NOME_POJO", beanName).replace("NOME_TABELA", nomeTabela) + "\r\n");
			}
		}
		ManipuladorDeArquivos.salvarArquivo(String.format(pastaDeSaida + "java\\br\\gov\\pe\\seres\\siap\\bean\\" + "%s.java", beanName),
				output.toString());
	}

	/**
	 * @param args
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException {
		String beanName = "RegimeCondenacaoSancao";
		String nomeClasse = "Regime/Condena\\u00E7\\u00E3o/San\\u00E7\\u00E3o";
		String nomeTabela = "regime_condenacao_sancao";
		String nomePojoAlias = "regimeCondenacaoSancao";
		String nomePackage = beanName.toLowerCase();
		boolean masculino = true;
		String umUma = masculino ? "um" : "uma";
		String oA = masculino ? "o" : "a";
		String atributoBean = beanName.substring(0, 1).toLowerCase() + beanName.substring(1);
		String pastaDeSaida = "C:\\Desenvolvimento\\workspaceDemoiselle\\siap\\src\\main\\";

		System.out.println("---------------POJO---------------");
		gerarPojo(beanName, nomeTabela, pastaDeSaida);
		System.out.println("---------------ENTRADAS DO CLASSES.PROPERTIES---------------");
		gerarEntradasClassesProperties(beanName, nomeClasse);
		System.out.println("---------------ENTRADAS DO PERSISTENCE.XML---------------");
		gerarEntradasPersistenceXml(beanName);
		System.out.println("---------------INTERFACE DAO---------------");
		gerarInterfaceDAO(beanName, pastaDeSaida);
		System.out.println("---------------CLASSE DAO---------------");
		gerarClasseDAO(beanName, pastaDeSaida);
		System.out.println("---------------INTERFACE BC---------------");
		gerarInterfaceBC(beanName, pastaDeSaida);
		System.out.println("---------------CLASSE BC---------------");
		gerarClasseBC(beanName, masculino, pastaDeSaida);
		System.out.println("---------------FILTRO---------------");
		gerarFiltro(beanName, pastaDeSaida);
		System.out.println("---------------EDIT MB---------------");
		gerarEditMB(beanName, atributoBean, nomePojoAlias, nomePackage, pastaDeSaida);
		System.out.println("---------------LIST MB---------------");
		gerarListMB(beanName, atributoBean, nomePojoAlias, nomePackage, pastaDeSaida);
		System.out.println("---------------LIST XHTML---------------");
		gerarListXHTML(beanName, atributoBean, pastaDeSaida, nomeTabela);
		System.out.println("---------------EDIT XHTML---------------");
		gerarEditXHTML(beanName, atributoBean, pastaDeSaida, nomeTabela);
		System.out.println("---------------ENTRADAS DO MENU---------------");
		gerarEntradasMenu(beanName, nomeTabela, atributoBean);
		System.out.println("---------------ENTRADAS DO MESSAGES---------------");
		gerarEntradasMessagesProperties(atributoBean, nomeClasse, nomeTabela);
		System.out.println("---------------ENTRADAS DA FACHADA---------------");
		gerarEntradasFachada(beanName, atributoBean, umUma, oA);
		System.out.println("---------------ENTRADAS DO ALIAS---------------");
		gerarEntradasAlias(beanName, atributoBean, nomePojoAlias);
		System.out.println("---------------ENTRADAS DO FACES-CONFIG---------------");
		gerarEntradasFacesConfig(beanName, atributoBean);
		System.out.println("---------------LEMBRAR CRIA A CARGA INICIAL---------------");
	}
}
